package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductResult;
import cn.itcast.vo.ExportResult;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;


@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ExportProductDao exportProductDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ExtEproductDao extEproductDao;

    /**
     * 报运单分页
     * @param exportExample 分页查询的参数
     * @param pageNum 当前页
     * @param pageSize 页大小
     * @return
     */
    @Override
    public PageInfo<Export> findByPage(ExportExample exportExample, int pageNum, int pageSize) {
        //设置开始页与页面大小
        PageHelper.startPage(pageNum, pageSize);
        //查询全部
        List<Export> exportList = exportDao.selectByExample(exportExample);
        PageInfo<Export> pageInfo = new PageInfo<>(exportList);
        return pageInfo;
    }
    //根据海关返回的结果，把结果的信息同步到我们的数据库中
    @Override
    public void updateByResult(ExportResult exportResult) {
        //找到报运单
        String exportId = exportResult.getExportId();
        Export export = exportDao.selectByPrimaryKey(exportId);
        //更新报运单的状态
        export.setState(exportResult.getState());
        //更新报运单的备注信息
        export.setRemark(exportResult.getRemark());
        //更新报运单
        exportDao.updateByPrimaryKeySelective(export);
        Set<ExportProductResult> products = exportResult.getProducts();
        for (ExportProductResult product : products) {
            String exportProductId = product.getExportProductId();
            ExportProduct exportProduct = exportProductDao.selectByPrimaryKey(exportProductId);
            exportProduct.setTax(product.getTax());
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }


    }

    @Override
    public List<Export> findAll(ExportExample exportExample) {
        return exportDao.selectByExample(exportExample);
    }

    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
       export.setId(UUID.randomUUID().toString());
       //将报运单补全
       export.setInputDate(new Date());
       //找到该报运单下所有购销合同
        String[] contractIds = export.getContractIds().split(",");
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andIdIn(Arrays.asList(contractIds));
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        //定义一个变量存储合同号
        String contractNos="";
        //存储所有的购销合同的货物种类属性
        int totalProNum=0;
        //存储所有购销合同的附件种类属性
        int totalExtNum=0;
        for (Contract contract : contractList) {
            contractNos+=contract.getContractNo()+" ";
            totalExtNum+=contract.getExtNum();
            totalProNum+=contract.getProNum();
            contract.setState(2);
            contractDao.updateByPrimaryKeySelective(contract);
        }
        //把合同号设置到报单表
        export.setCustomerContract(contractNos);
        export.setState(0);
        export.setExtNum(totalExtNum);
        export.setProNum(totalProNum);
        export.setCreateTime(new Date());
        export.setUpdateTime(new Date());
        exportDao.insertSelective(export);

        //把购销合同下所有货物数据导入到报运商品表中
        ContractProductExample contractProductExample = new ContractProductExample();
        //添加购销合同下的货物添加
        contractProductExample.createCriteria().andContractIdIn(Arrays.asList(contractIds));
        List<ContractProduct> contractProductList = contractProductDao.selectByExample(contractProductExample);
        //定义一个map集合存储，后效合同货物的id与报运货物的id
        HashMap<String, String> map = new HashMap<>();
        for (ContractProduct contractProduct : contractProductList) {
            //一个购销合同的货物就是对应一个报运单的商品数据
            ExportProduct exportProduct = new ExportProduct();
            BeanUtils.copyProperties(contractProduct,exportProduct);
            //报运商品的id
            exportProduct.setId(UUID.randomUUID().toString());
            //报运商品所属的包运单
            exportProduct.setExportId(export.getId());
            map.put(contractProduct.getId(),exportProduct.getId());
            //把报运商品插入到数据库
            exportProductDao.insertSelective(exportProduct);

        }
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andIdIn(Arrays.asList(contractIds));
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        for (ExtCproduct extCproduct : extCproducts) {
            ExtEproduct extEproduct = new ExtEproduct();
            BeanUtils.copyProperties(extCproduct,extEproduct);
            extEproduct.setId(UUID.randomUUID().toString());
            extEproduct.setExportId(export.getId());
            extEproduct.setExportProductId(map.get(extCproduct.getContractProductId()));
            //插入数据
            extEproductDao.insertSelective(extEproduct);
        }
    }

    @Override //保存报运单
    public void update(Export export) {
        //更新包运单
        export.setUpdateTime(new Date());
        exportDao.updateByPrimaryKeySelective(export);
        //更新报运商品
        List<ExportProduct> exportProducts = export.getExportProducts();
        if (exportProducts!=null) {
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }



}
